/* enum */
import { BusinessServiceCloudRoleIdEnum, CustomerServiceCloudRoleIdEnum, OldSHBRoleIdEnum, RoleIdEnum } from "@model/enum/RoleEnum"
/* model */
import Role, { RoleAuthDepthEnum, RoleAuthGroup, RoleStateEnum, RoleType, RoleTypeEnum } from "@model/entity/Role/Role"
/* util */
import { isNumber, isString } from "@src/util/type"
import { isAnySystemAdminByRoleId } from "@shb-lib/tenant"

/** 
 * @description 是否为系统管理员
*/
function isSystemAdmin(role: Role): boolean {
  return isAnySystemAdminByRoleId(role?.id)
}

/** 
 * @description 是否不为系统管理员
*/
function isNotSystemAdmin(role: Role): boolean {
  return !isSystemAdmin(role)
}

/** 
 * @description 是否为系统角色
 * @return role.isSystem == 1
*/
function isSystemRole(role: RoleType): boolean {
  return role.isSystem == 1
}

function isSystemAdminRole(role: RoleType): boolean {
  const id = Number(role?.id)
  return id === Number(RoleIdEnum.SystemAdmin) || id === Number(RoleIdEnum.BscSystemAdmin) || id === Number(RoleIdEnum.CscSystemAdmin)
}

function isNotSystemAdminRole(value: Partial<RoleType> | string | number): boolean {
  
  const role = (isString(value) || isNumber(value)) ?  { id: value } : value
  
  return !isSystemAdminRole(role as RoleType)
}

/** 
 * @description 是否为自定义角色
 * @return role.isSystem !== 1
*/
function isCustomRole(role: RoleType): boolean {
  return !isSystemRole(role)
}

/** 
 * @description 是否有当前权限的所有权限
*/
function isHaveRoleAllAuth(value?: number): boolean {
  return value == RoleAuthDepthEnum.All
}

/** 
 * @description 是否有当前权限的所有权限
*/
function isHaveRolAuth(value?: number): boolean {
  return Boolean(value)
}

function isRoleTypeParentTag(roleType: RoleTypeEnum): boolean {
  return roleType == RoleTypeEnum.ParentTag
}

function isRoleTypeToBeAssigned(roleType: RoleTypeEnum): boolean {
  return roleType == RoleTypeEnum.ToBeAssigned
}

/** 
 * @return roleType == 'assigned'
*/
function isRoleTypeAssigned(roleType: RoleTypeEnum): boolean {
  return roleType == RoleTypeEnum.Assigned
}

function isRoleEdited(role: RoleType): boolean {
  return role?.state == RoleStateEnum.Edited
}

function isRoleUnEdited(role: RoleType): boolean {
  return role?.state == RoleStateEnum.UnEdited
}

function roleAuthSort(auths: RoleAuthGroup[]): RoleAuthGroup[] {
  
  const authsLength = auths.length
  const authsHalfLength = Math.floor(authsLength / 2)
  
  let leftAuths: RoleAuthGroup[] = auths.slice(0, authsHalfLength)
  let rightAuths: RoleAuthGroup[] = auths.slice(authsHalfLength, authsLength)
  
  let newAuths: RoleAuthGroup[] = []
  
  rightAuths.forEach((rightAuth, index) => {
    
    let leftAuth: RoleAuthGroup | undefined = leftAuths[index]
    
    if (leftAuth) {
      newAuths.push(leftAuth)
    }
    
    newAuths.push(rightAuth)
    
  })
  
  return newAuths
}

export {
  isSystemAdmin,
  isNotSystemAdmin,
  isSystemRole,
  isCustomRole,
  isHaveRoleAllAuth,
  isRoleTypeParentTag,
  isRoleTypeToBeAssigned,
  isRoleTypeAssigned,
  isRoleUnEdited,
  isRoleEdited,
  isNotSystemAdminRole,
  roleAuthSort,
  isHaveRolAuth
}

export default {
  isSystemAdmin,
  isNotSystemAdmin,
  isSystemRole,
  isCustomRole,
  isHaveRoleAllAuth,
  isRoleTypeParentTag,
  isRoleTypeToBeAssigned,
  isRoleTypeAssigned,
  isRoleUnEdited,
  isRoleEdited,
  isNotSystemAdminRole,
  roleAuthSort,
  isHaveRolAuth
}